<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button v-has="add" size="small" type="primary" @click="showAddDialog">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="头像" sortable="" width="100">
            <template v-slot="{row}">
              <img v-imgError="require('@/assets/head.jpg')" :src="row.staffPhoto" alt="" style="height:100%;width:100%;borderRadius:100%" @click="showQrCode(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry" />
          <el-table-column label="账户状态" sortable="" prop="enableState" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="assignRole(row)">角色</el-button>
              <el-button v-if="checkPermission(remove)" type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="total, prev, pager, next"
            :page-size="page.size"
            :current-page="page.page"
            :total="page.total"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <add-employees :visible.sync="showAdd" :hire-type="hireType" @getEmployeeList="getEmployeeList" />

    <el-dialog
      title="提示"
      :visible.sync="qrCodeVisible"
      width="30%"
    >
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>

    <AssignRole :visible.sync="showAssign" :employee-id="employeeId" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import employee from '@/constant/employee.js'
const { exportExcelMapPath, hireType } = employee
import AddEmployees from './components/AddEmployees.vue'
import AssignRole from './components/AssignRole.vue'
import headImg from '@/assets/head.jpg'
import QrCode from 'qrcode'
import permissionEnum from '@/constant/permission'
const { remove, add, update } = permissionEnum
import checkPermission from '@/mixin/check-permission'
export default {
  components: {
    AddEmployees,
    AssignRole
  },
  mixins: [checkPermission],
  data() {
    return {
      headImg,
      page: {
        page: 1, // 当前页码
        size: 10,
        total: 0 // 总数
      },
      loading: false,
      list: [],
      showAdd: false,
      hireType,
      qrCodeVisible: false,
      showAssign: false,
      employeeId: '',
      remove,
      add,
      update
    }
  },

  created() {
    this.getEmployeeList()
  },

  methods: {
    async getEmployeeList() {
      this.loading = true
      const { total, rows } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    formatEmployment(row, column, cellValue, index) {
      const item = hireType.find(i => i.id === cellValue)?.value
      return item || '未知'
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    async delEmployee(id) {
      try {
        await this.$confirm('你确定要删除该员工？')
        await delEmployee(id)
        this.$message.success('删除成功')
        this.getEmployeeList()
      } catch (e) {
        console.log(e)
      }
    },
    showAddDialog() {
      this.showAdd = true
    },
    async exportExcel() {
      const excel = await import('@/vendor/Export2Excel')
      const { rows } = await getEmployeeList({ page: 1, size: this.page.total })
      const header = Object.keys(exportExcelMapPath)
      const data = rows.map(row => {
        return header.map(h => {
          if (h === '聘用形式') {
            return hireType.find(hire => hire.id === row[exportExcelMapPath[h]])
              ?.value || '未知'
          }
          return row[exportExcelMapPath[h]]
        })
      })
      await excel.export_json_to_excel({
        header,
        data,
        filename: '员工信息表'
      })
    },
    showQrCode(url) {
      if (url) {
        this.qrCodeVisible = true
        this.$nextTick(() => {
          // 此时可以确认已经有ref对象了
          QrCode.toCanvas(this.$refs.myCanvas, url) // 将地址转化成二维码
          // 如果转化的二维码后面信息 是一个地址的话 就会跳转到该地址 如果不是地址就会显示内容
        })
      } else {
        this.$message.warning('该用户还没上传头像')
      }
    },
    assignRole(row) {
      this.employeeId = row.id
      this.showAssign = true
    }
  }
}
</script>

<style scoped lang="less"></style>
